<?php

namespace App\Admin\Controllers;

use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\Activity;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;

class ActivityController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('活动列表');
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return  AdminC::grid(Activity::class,function(Grid $grid){
            $grid->filter(function($filter){
                $model = new Activity();
                $filter->equal('location','位置')->select($model->location_arr);
                $filter->equal('status','状态')->select($model->status_arr);
            });
            if(!Input::has('_sort')) {
                $grid->model()->orderby('id','desc');
            }
            $grid->id('Id')->sortable();
            $grid->location('位置')->display(function($location){
                return $this->location_arr[$location];
            });
            $grid->sort('排序');
            $grid->view_status('状态')->display(function($view_status){
                return $this->view_status_arr[$view_status];
            });
            $grid->title('标题');
            $grid->url('链接');
            $grid->pic('图片')->display(function($pic){
                return '<img src="/uploads/'.$pic.'" width="150" height="80"/>';
            });

            $grid->disableExport();
        });
    }

    public function create()
    {
        return AdminC::content(function(Content $content){
            $content->header('活动列表');
            $content->description('新增');
            $content->body($this->form());
        });
    }

    public function edit($id)
    {
        return AdminC::content(function(Content $content) use($id){
            $content->header('活动列表');
            $content->description('新增');
            $content->body($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return AdminC::form(Activity::class, function(Form $form){
            $form->display('id','Id');
            $form->text('title','标题');
            $form->select('location','位置')->options($form->model()->location_arr);
            $form->text('url','链接地址');
            $form->image('pic','图片');
            $form->text('sort', '排序');
            $form->select('status','状态')->options($form->model()->status_arr);
            $form->select('view_status','显示方式')->options($form->model()->view_status_arr);
            $form->display('created_at','创建时间');
            $form->display('updated_at','更新时间');

        });
    }


}
